package io.renren.controller;

import io.renren.annotation.LoginUser;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.dto.SupplierDTO;
import io.renren.entity.UserEntity;
import io.renren.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 供应商表
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-11-22
 */
@RestController
@RequestMapping("api/supplier")
@Api(tags="食材采购")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    @GetMapping("page")
    @ApiOperation("供应商信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result< ArrayList<SupplierDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        params.put("limit","50");
        PageData<SupplierDTO> page = supplierService.page(params);
        ArrayList<SupplierDTO> list = new ArrayList<>();
        for (SupplierDTO supplierDTO:page.getList() ) {//获取当前的供应食堂的字段
            String[] split = supplierDTO.getCanteenId().split(",");// 用,分割;
                for (String canteenId : split) {//判断当前供应商是否供应此食堂
                    if (entity.getDeptId().toString().equals(canteenId)) {
                        list.add(supplierDTO);
                    }
                }

        }
        return new Result< ArrayList<SupplierDTO>>().ok(list);
    }

}